package com.example.wanandroid.service.impl;

import com.example.wanandroid.mapper.TbCollectionMapper;
import com.example.wanandroid.mapper.ViewMapper;
import com.example.wanandroid.pojo.TbCollection;
import com.example.wanandroid.service.CollectionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @Desc TODO
 * @Date 2020/4/19 14:05
 */
@Service
public class CollectionServiceImpl implements CollectionService {
    @Autowired
    TbCollectionMapper collectionMapper;
    @Autowired
    ViewMapper viewMapper;

    @Override
    public int deleteByPrimaryKey(Long id) {
        return collectionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(TbCollection record) {
        return collectionMapper.insertSelective(record);
    }

    @Override
    public TbCollection selectByPrimaryKey(Long id) {
        return collectionMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKey(TbCollection record) {
        return collectionMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public TbCollection selectByQuestionUserId(Long questionId, long userid) {
        return collectionMapper.selectByQuestionUserId(questionId, userid);
    }


    @Override
    public PageInfo<HashMap<String, Object>> getByCollectionList(long userId, int page, int pageSize) {
        List<HashMap<String, Object>> listQ = new ArrayList<>();
        //设置分页信息
        PageHelper.startPage(page, pageSize);
        List<TbCollection> cList = collectionMapper.getByCollectionList(userId);

        for (TbCollection collection : cList) {
            Long questionId = collection.getQuestionId();
            HashMap<String, Object> question = viewMapper.getQuestionById(questionId);
            listQ.add(question);
        }
        //获取分页信息
        PageInfo<HashMap<String, Object>> pageInfo = new PageInfo<>(listQ);
        return pageInfo;
    }
}
